package com.longteng.diamond.service.impl;


import com.longteng.diamond.dao.InterfaceTestMapper;
import com.longteng.diamond.dao.ProjectHostMapper;
import com.longteng.diamond.domain.InterfaceTest;
import com.longteng.diamond.domain.ProjectShareNode;
import com.longteng.diamond.po.RequestResult;
import com.longteng.diamond.service.ProjectShareInterface;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by Administrator
 * DATE : 2018/7/28
 * TIME : 13:02
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
@Component
public class ProjectHostInterface implements ProjectShareInterface {
    @Autowired
    ProjectHostMapper projectHostMapper;
    @Autowired
    InterfaceTestMapper interfaceTestMapper;

    /**
     * 新增host
     *
     * @param projectShareNode
     * @return
     */
    @Override
    public RequestResult addProjectShare(ProjectShareNode projectShareNode) {
        RequestResult requestResult = this.checkHost(projectShareNode);
        if (!requestResult.isSuccess()) {
            return requestResult;
        }
        projectHostMapper.addProjectShareNode(projectShareNode);
        requestResult.setSuccess(true);
        return requestResult;
    }

    /**
     * 修改host
     *
     * @param projectShareNode
     * @return
     */
    @Override
    public RequestResult updateProjectShare(ProjectShareNode projectShareNode) {
        RequestResult requestResult = this.checkHost(projectShareNode);
        if (!requestResult.isSuccess()) {
            return requestResult;
        }
        projectHostMapper.updateProjectShareNode(projectShareNode);
        requestResult.setSuccess(true);
        return requestResult;
    }


    /**
     * 校验host是否合法
     * @param projectShareNode
     * @return RequestResult
     */
    private RequestResult checkHost(ProjectShareNode projectShareNode) {
        RequestResult requestResult = new RequestResult();
        requestResult.setSuccess(true);
        String domain = projectShareNode.getDomain();
        String ip = projectShareNode.getIp();
        if (StringUtils.isBlank(ip)) {
            requestResult.setSuccess(false);
            requestResult.setMsg("ip不能为空!");
            return requestResult;
        } else {
            if (!this.ipCheck(ip)) {
                requestResult.setSuccess(false);
                requestResult.setMsg("请输入合法ip!");
                return requestResult;
            }
        }
        if (StringUtils.isBlank(domain)) {
            requestResult.setSuccess(false);
            requestResult.setMsg("域名不能为空!");
            return requestResult;
        }
        ProjectShareNode projectShareNode1 = projectHostMapper.getHostByDomain(projectShareNode);
        if (null != projectShareNode1) {
            Integer dbId = projectShareNode1.getId();
            Integer id = projectShareNode.getId();
            if (null == id || id.intValue() != dbId.intValue()) {
                requestResult.setSuccess(false);
                requestResult.setMsg("host【" + domain + "】已经存在!");
                return requestResult;
            }
        }
        return requestResult;
    }

    /**
     * 判断IP地址的合法性，这里采用了正则表达式的方法来判断
     * @param ip return true，合法
     * @return  boolean
     */
    public boolean ipCheck(String ip) {
        if (ip != null && !ip.isEmpty()) {
            // 定义正则表达式
            String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." +
                    "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." +
                    "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." +
                    "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
            // 判断ip地址是否与正则表达式匹配
            if (ip.matches(regex)) {
                // 返回判断信息
                return true;
            } else {
                // 返回判断信息
                return false;
            }
        }
        return false;
    }

    /**
     * 校验host是否绑定接口了
     * @param id
     * @return
     */
    @Override
    public RequestResult deleteProjectShareCheck(Integer id) {
        RequestResult requestResult = new RequestResult();
        InterfaceTest test=new InterfaceTest();
        test.setHostId(id);
        List<InterfaceTest> interfaceTestList = interfaceTestMapper.getInterfaceTest(test);
        if (interfaceTestList.size() > 0) {
            requestResult.setSuccess(false);
            requestResult.setMsg("该host已经绑定接口,会同时删除绑定的接口,确定删除吗?");
        } else {
            requestResult.setSuccess(true);
        }
        return requestResult;
    }

    /**
     * 删除host
     * @param id
     * @return
     */
    @Override
    public RequestResult deleteProjectShare(Integer id) {
        RequestResult requestResult=new RequestResult();
        projectHostMapper.deleteProjectShareNode(id);
        interfaceTestMapper.updateInterfaceTestHostId(id);
        requestResult.setSuccess(true);
        return requestResult;
    }
    /**
     * 删除host
     * @param projectId
     * @return
     */
    public List<ProjectShareNode> getHostForInterface(Integer projectId) {
        List<ProjectShareNode> projectShareNodeList=  projectHostMapper.getHostByProjectId(projectId);
        return projectShareNodeList;
    }

    public static void main(String[] args) {
         //       List<String> domainsList = JsonPath.read(projectShareNode1.getHosts(), expression);
        String expression = "$[*].value";
    }
}
